{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a2edc535-1f4a-43b1-ac6c-8dbd2c884420",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4534833b-f4d2-4ca6-8e42-09165c1d6320",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fang_array = np.array([[1,2,3], [4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "15daa091-3674-488b-85bd-62a4b3e3b4c2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "np.savetxt(\"fang2.txt\", fang_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c1d2587-03dc-4097-ad86-678fc71cdbb1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#保存为整数\n",
    "np.savetxt(\"fang2.txt\", fang_array, fmt=\"%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "863ba8b3-42a3-4461-a36e-a63e49257e50",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "np.savetxt(\"fang2.txt\", fang_array, \n",
    "          fmt=\"%.2f\", delimiter=\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33244381-5f4a-4ece-8942-34bb5648d5ef",
   "metadata": {},
   "source": [
    "___读写array结构___"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5d427d66-4c1f-4088-a6b3-b17b13e3aa25",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fang_array = np.array([[1,2,3], [4,5,6]])\n",
    "np.save(\"fang_array.npy\", fang_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8e5ab41c-7a03-497e-9685-aa7c2c822387",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang = np.load(\"fang_array.npy\")\n",
    "fang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "15a96231-2271-4fcf-88bd-cd306366724e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fang_array2 = np.arange(10)\n",
    "#生成一个压缩包\n",
    "np.savez(\"fang.npz\", a=fang_array, b=fang_array2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f77a6d7c-439c-4854-ae6e-66288c6542cd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Mapping.keys of <numpy.lib.npyio.NpzFile object at 0x7f535c0f7b50>>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.load(\"fang.npz\")\n",
    "data.keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ef553c4c-7163-4fa0-b571-9a2208968af7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"a\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b67813b-b92a-4a7e-98bd-1549215dad88",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
